﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NewsInfo; 

public partial class edit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        NewsInfoBindData();
        Operate();
        tbxNewsDate.Text = DateTime.Now.ToString();
    }
    protected void NewsInfoBindData()
    {
        AspNetPager1.RecordCount = NewsInfoControl.GetCount(null);
        System.Collections.Generic.List<NewsInfo.NewsInfo> hhh = NewsInfoControl.GetList(AspNetPager1.StartRecordIndex, AspNetPager1.PageSize, null);
        Repeater1.DataSource = hhh;
        Repeater1.DataBind();
    }
    protected void AspNetPager1_PageChanged(object src, EventArgs e)
    {
        NewsInfoBindData();
    }

    protected void btnInsert_Click(object sender, EventArgs e)
    {
        NewsInfo.NewsInfo hhh = new NewsInfo.NewsInfo();
        hhh.NewsTitle = tbxNewsTitle.Text;
        hhh.NewsData = tbxNewsData.Text;
        hhh.NewsImageUrl = tbxNewsimg.Text;
        hhh.NewsDate = Convert.ToDateTime(tbxNewsDate.Text);
        hhh.NewsCategory = (NewsCatgory.NewsCatgoryType)Enum.Parse(typeof(NewsCatgory.NewsCatgoryType), rblType.SelectedItem.Value, true);
        NewsInfoControl.Insert(hhh);
        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "提示", "alert('添加成功');location.href=location.href", true);
    }
    protected void Operate()
    {
        int cid = 0;
        if (Request["deleteId"] != null && int.TryParse(Request["deleteId"], out cid) && cid > 0)
        {
            NewsInfoControl.Delete(cid);
            Response.Redirect("edit.aspx");

        }
        if (Request["editId"] != null && int.TryParse(Request["editId"], out cid) && cid > 0)
        {
            NewsInfo.NewsInfo hhh = NewsInfoControl.GetModel(cid);
            if (hhh != null && !Page.IsPostBack)
            {
                tbxNewsTitle.Text = hhh.NewsTitle;
                tbxNewsData.Text = hhh.NewsData;
                tbxNewsimg.Text = hhh.NewsImageUrl;
            }
        }
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        int cid = 0;

        if (Request["editId"] != null && int.TryParse(Request["editId"], out cid) && cid > 0)
        {
            NewsInfo.NewsInfo hhh = NewsInfoControl.GetModel(cid);
            if (hhh != null)
            {
                hhh.NewsTitle = tbxNewsTitle.Text;
                hhh.NewsData = tbxNewsData.Text;
                hhh.NewsImageUrl = tbxNewsimg.Text;

                NewsInfoControl.Update(hhh);
                Response.Redirect("edit.aspx");

            }
        }
    }
}